package com.usian.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.Result;
import com.usian.common.ResultCodeEnum;
import com.usian.dto.OrderDto;
import com.usian.entity.Orders;
import com.usian.order.service.IOrdersService;
import com.usian.vo.ReserveVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/order/orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    @Operation(summary = "根据用户id查询订单数据")
    @GetMapping("/getOrderByUserId/{userId}")
    public Result<Orders> getOrderByUserId(@PathVariable Integer userId){
        return ordersService.getOrderByUserId(userId);
    }

    @Operation(summary = "根据用户id查询已经预定订单")
    @GetMapping("/findAllBooked/{userId}")
    Result<Orders> findAllBooked(@PathVariable Integer userId){
        return ordersService.findAllBooked(userId);
    }

    @Operation(summary = "根据用户id查询已支付订单")
    @GetMapping("/findAllzhifu/{userId}")
    Result<Orders> findAllzhifu(@PathVariable Integer userId){
        return ordersService.findAllzhifu(userId);
    }


    @Operation(summary = "根据用户id查询历史订单")
    @GetMapping("/findOrderHistory/{userId}")
    public Result<List<Orders>> findOrderHistory(@PathVariable Integer userId){
        //根据userid查询所有订单
        return Result.build(ordersService.findOrderHistory(userId), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据用户id查询所有订单")
    @GetMapping("/findAllList/{userId}")
    public Result<List<Orders>> findAllList(@PathVariable Integer userId) {
        // 优化：使用 MyBatis-Plus 的 LambdaQueryWrapper 进行查询，根据订单开始时间倒序
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.<Orders>lambdaQuery()
                .eq(Orders::getUserId, userId)  // 筛选订单状态为已预定
                .orderByDesc(Orders::getUpdatedAt);  // 根据订单开始时间倒序排序
        // 根据条件查询订单列表
        List<Orders> ordersList = ordersService.list(queryWrapper);
        System.out.println(ordersList);
        return Result.build(ordersList, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加订单")
    @PostMapping("/addOrders")
    public Result<Boolean> addOrders(@RequestBody OrderDto orderDto){
        return Result.build(ordersService.addOrders(orderDto), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改订单状态")
    @PutMapping("/updateOrder")
    public Result<Boolean> updateOrder(@RequestBody Orders orders){
        //修改订单状态
        return Result.build(ordersService.updateById(orders), ResultCodeEnum.SUCCESS);
    }



}
